import { login, logout, getInfo } from '@/api/auth'
import { hex_sha1 } from '@/utils/sha1'
import {setUserName,removeUserName,getUserName} from '@/utils/cookie'

const state = {
    userName:'anonymousUser',
    roles:['ROLE_ANONYMOUS'],
    loginStatus:false,
    checkLogin:true,
    url:''
}

const getters = {

}

const actions = {
    //{ state, commit, rootState }
    login( {commit}, userInfo){

        const { username, password } = userInfo
       // console.log(hex_sha1)
        let sha1Pwd = hex_sha1(password).toLowerCase()
        //console.log(sha1Pwd)
        return new Promise((resolve, reject) => {

            login({ username: username.trim(), password: sha1Pwd }).then(response => {
                const { data } = response
                if( data.error == 0 ){
                    //login success
                    data['userName'] = username
                    commit('SET_LOGININFO', data)
                    resolve(data)
                }else{
                    //login failure
                    commit('RESET_LOGININFO')
                    reject(new Error('登录失败！'));
                }
            }).catch(error => {
                reject(error)
            })
        })
    },
    logout(ctx, userInfo){

        return new Promise((resolve, reject) => {

            logout({ username: username.trim(), password: sha1Pwd }).then(response => {
                const { data } = response
                if( data.error == 0 ){
                    //login success
                    data['userName'] = username
                    commit('SET_LOGININFO', data)
                    resolve(data)
                }else{
                    //login failure
                    commit('RESET_LOGININFO')
                    reject(new Error('登录失败！'));
                }
            }).catch(error => {
                reject(error)
            })
        })
    },
    checkLogin( {commit}){

        commit('SET_CHECKLOGIN', false)
        return new Promise((resolve, reject) => {

            getInfo().then(response => {
                const { data } = response
                if( data.error == 0 ){
                    //login success
                    commit('SET_LOGININFO', data)
                    resolve(data)
                }else{
                    //login failure
                    commit('RESET_LOGININFO')
                    reject(new Error('登录失败！'));
                }
            }).catch(error => {
                reject(error)
            })
        })
    }
}

const mutations = {
    SET_LOGININFO: (state, userInfo) => {

        state.userName = userInfo.userName;
        state.roles = userInfo.roles;
        state.loginStatus = true;
        state.url = userInfo.url;
        //setUserName(userInfo.userName);
    },
    RESET_LOGININFO: (state) => {
        state.userName = 'anonymousUser';
        state.roles = ['ROLE_ANONYMOUS'];
        state.loginStatus = false;
        state.url = '';
        //removeUserName();
    },
    SET_CHECKLOGIN:(state, status) => {

        state.checkLogin = status;
    },
}

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}